﻿#include "PhysicochemicalAnalyse.h"
#include "DatabaseMgmt.h"

CPhysicochemicalAnalyse::CPhysicochemicalAnalyse()
{

}

CPhysicochemicalAnalyse::~CPhysicochemicalAnalyse()
{

}

int CPhysicochemicalAnalyse::queryByReportUid(uint32_t report_id, PhysicochemicalAnalyse& data)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("SELECT UID,REPORT_UID,ELEMENT_TEST,ANALYSE_KIND,RADIAL_TEST"
		" FROM TB_PHYSICOCHEMICAL_ANALYSE WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}
	else
	{
		if (query.next())
		{
			data.uid = query.value("UID").toUInt();
			data.report_uid = query.value("REPORT_UID").toUInt();
			data.element_test = query.value("ELEMENT_TEST").toString();
			data.analyse_kind = query.value("ANALYSE_KIND").toUInt();
			data.radial_test = query.value("RADIAL_TEST").toString();
		}
	}

	return ret;
}

int CPhysicochemicalAnalyse::removeByReportUid(uint32_t report_id)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("DELETE FROM TB_PHYSICOCHEMICAL_ANALYSE WHERE REPORT_UID=%1")
		.arg(report_id);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CPhysicochemicalAnalyse::modifyByUid(PhysicochemicalAnalyse data)
{
	int ret = CONST_ERR_SUCCESS;

	QString strSql = QString("UPDATE TB_PHYSICOCHEMICAL_ANALYSE SET REPORT_UID=%1,ELEMENT_TEST='%2',ANALYSE_KIND=%3,"
		" RADIAL_TEST='%4' WHERE UID=%6")
		.arg(data.report_uid)
		.arg(data.element_test)
		.arg(data.analyse_kind)
		.arg(data.radial_test)
		.arg(data.uid);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}

int CPhysicochemicalAnalyse::insertRow(PhysicochemicalAnalyse data)
{
	int ret = 0;

	QString strSql = QString("INSERT INTO TB_PHYSICOCHEMICAL_ANALYSE (REPORT_UID,ELEMENT_TEST,ANALYSE_KIND,RADIAL_TEST)"
		" VALUES (%1,'%2',%3,'%4')")
		.arg(data.report_uid)
		.arg(data.element_test)
		.arg(data.analyse_kind)
		.arg(data.radial_test);

	QSqlQuery query;
	CDatabaseMgmt::getInstance()->execSql(strSql, query);

	if (query.lastError().type() != QSqlError::NoError)
	{
		ret = CONST_ERR_EXEC_SQL_FAILED;
		qInfo() << query.lastError().text();
	}

	return ret;
}
